package com.free4inno.pagesite.controller;

import com.free4inno.pagesite.utils.ssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
public class IndexController {

    /**
     * 前往首页
     * （返回首页页面）
     */

    //配置搜索参数
    @Value("${zhi.site.advancedSearchKey}")
    private String[] advancedSearchKey;

    @Value("${zhi.site.advancedSearchValue}")
    private String[] advancedSearchValue;

    @Value("${zhi.site.classificationSearchKey}")
    private String [] classificationSearchKey;

    @Value("${zhi.site.classificationSearchValue}")
    private String [] classificationSearchValue;

    @Value("${zhi.site.hotSearchKey}")
    private String [] hotSearchKey;

    @Value("${zhi.site.hotSearchValue}")
    private String [] hotSearchValue;

    @Value("${zhi.site.advancedSearchText}")
    private  String advancedSearchText;

    @Value("${zhi.site.classificationSearchText}")
    private  String classificationSearchText;

    @Value("${zhi.site.hotSearchText}")
    private  String hotSearchText;

    @Value("${zhi.site.defaultSearchTerms}")
    private  String defaultSearchTerms;

    @Value("${zhi.site.title}")
    private  String title;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        List<ssUtils> advancedSearch  =new ArrayList<>();
        List<ssUtils> classificationSearch  =new ArrayList<>();
        List<ssUtils> hotSearch  =new ArrayList<>();

        request.setCharacterEncoding("UTF-8");
        for (int i =0;i<advancedSearchKey.length;i ++){
            ssUtils s = new ssUtils();
            s.setKey(advancedSearchKey[i]);
            s.setValue(advancedSearchValue[i]);
            advancedSearch.add(s);
        }
        for (int i =0;i<classificationSearchKey.length;i ++){
            ssUtils s = new ssUtils();
            s.setKey(classificationSearchKey[i]);
            s.setValue(classificationSearchValue[i]);
            classificationSearch.add(s);
        }
        for (int i =0;i<hotSearchKey.length;i ++){
            ssUtils s = new ssUtils();
            s.setKey(hotSearchKey[i]);
            s.setValue(hotSearchValue[i]);
            hotSearch.add(s);
        }
        request.setAttribute("advancedSearch",advancedSearch);
        request.setAttribute("classificationSearch",classificationSearch);
        request.setAttribute("hotSearch",hotSearch);
//        for (int i =0;i<arrkey.length;i ++){
//            ssUtils s = new ssUtils();
//            s.setKey(arrkey[i]);
//            s.setVa(arrvalue[i]);
//            listone.add(s);
//        }

//        request.setAttribute("classificationSearch",arr2key);
//        request.setAttribute("advancedSearch",listone);
//        request.setAttribute("list",list);
        request.setAttribute("advancedSearchText",advancedSearchText);
        request.setAttribute("classificationSearchText",classificationSearchText);
        request.setAttribute("hotSearchText",hotSearchText);
        request.setAttribute("defaultSearchTerms",defaultSearchTerms);
        request.setAttribute("title",title);
        return "index/index";
    }
}
